package com.chookss.home.logContacts;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.view.ViewConfigurationCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.home.entity.ContactEntity;
import com.chookss.home.entity.LogEntity;
import com.chookss.home.publish.PublishLogsActivity;
import com.chookss.mine.personal.PersonalHomeActivity;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.NavigationTools;
import com.chookss.tools.PermissionSpUtils;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.OkGoUtil;
import com.chookss.tools.okgoconfig.callback.JsonCallback;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.BottomListsDialog;
import com.chookss.view.CircleImageView;
import com.chookss.view.RecycleViewDivider;
import com.chookss.view.SelectDialogListener;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.johnrambo.ktea.common.klogutil.KLog;
import com.johnrambo.ktea.ktExtends.ShareUtils;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.annotations.Nullable;

@SuppressLint("ValidFragment")
public class LogFragment extends Fragment implements OnRefreshListener, OnLoadMoreListener {

    public Context mContext;
    Unbinder unbinder;
    @BindView(R.id.rvView)
    RecyclerView rv;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    private LogListAdapter adapter;
    private List<LogEntity> list = new ArrayList<>();
    private List<String> dialogList = new ArrayList<>();
    private int currentPage = 1;
    private String pageSize = "20";
    private boolean isEnd = false;//是否到底
    private String type;//0全部日志1我的日志
    private String reportType = "";// 0 日报 1 周报 2 月报 3 日志
    private String startTime = "", endTime = "";
    private String employeeCodes = "", orgCodes = "";//筛选的组织和人
    private String singleEmployeeCode = ""; //是否只看本人
    private BottomListsDialog bottomListsDialog = null;

    public LogFragment(String type) {
        this.type = type;
    }

    public LogFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_log, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void init() {
        //注册接受
        EventBus.getDefault().register(this);
        rv.setPadding(Utils.dip2px(mContext, 12), 0, Utils.dip2px(mContext, 12), 0);
        rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rv.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5f), getResources().getColor(R.color.line_gray)
        ));
        adapter = new LogListAdapter(R.layout.item_look_log, mContext, list);
        rv.setAdapter(adapter);
        dialogList.clear();
        if ("0".equals(type)) {
            dialogList.add("只看此人");
            dialogList.add("取消");
        } else {
            dialogList.add("修改");
            dialogList.add("删除");
        }
        if ("0".equals(type)) {
        } else {
            getLogData();
        }
    }

    private RefreshLayout refreshLayout;
    private boolean isFirst = false;

    @Override
    public void onRefresh(@androidx.annotation.NonNull final RefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
        isEnd = false;
        currentPage = 1;
        getLogData();
    }

    @Override
    public void onLoadMore(@androidx.annotation.NonNull final RefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
        if (isEnd) {//has next page
            MyToast.show("没有更多数据了");
            loadEnd();
        } else {
            currentPage++;
            getLogData();
        }
    }

    public void refreshData(String startTime, String endTime, String reportType, List<ContactEntity> selectList) {
        this.startTime = startTime;
        this.endTime = endTime;
        this.reportType = reportType;
        List<ContactEntity> selectLists = new ArrayList<>();
        selectLists.addAll(selectList);
        orgCodes = "";
        employeeCodes = "";
        for (int i = 0; i < selectLists.size(); i++) {
            if (!selectLists.get(i).isHalfSelect()) {
                if (selectLists.get(i).getType().equals("0")) {
                    if (Utils.isNull(orgCodes)) {
                        orgCodes = orgCodes + selectLists.get(i).getEmployeeCode();
                    } else {
                        orgCodes = orgCodes + "," + selectLists.get(i).getEmployeeCode();
                    }
                } else {
                    if (Utils.isNull(employeeCodes)) {
                        employeeCodes = employeeCodes + selectLists.get(i).getEmployeeCode();
                    } else {
                        employeeCodes = employeeCodes + "," + selectLists.get(i).getEmployeeCode();
                    }
                }
            }

        }
        isEnd = false;
        currentPage = 1;
        getLogData();
    }

    private void getLogData() {
        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        map.put("reportType", reportType);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("pageNum", currentPage + "");
        map.put("pageSize", pageSize + "");
        if ("1".equals(type)) {
            map.put("employeeCode", new ShareUtils().getString(mContext, StaticClass.EMPLOYEECODE, ""));
        } else {
            map.put("employeeCode", employeeCodes);
            map.put("orgCode", orgCodes);
        }
        MyHttpRequest.getDefault().postRequest(new TypeToken<LzyResponse<List<LogEntity>>>() {
        }.getType(), null, Urls.getReportList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<LogEntity>>() {
            @Override
            public void onSuccess(List<LogEntity> data) {
                if (currentPage == 1) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                if (rv == null || llNone == null) {
                    return;
                }
                int oldIndex = list.size();
                if (data != null && data.size() > 0) {
                    list.addAll(data);
                    adapter.notifyItemRangeChanged(oldIndex, data.size());
                    rv.setVisibility(View.VISIBLE);
                    llNone.setVisibility(View.GONE);
                } else {
                    isEnd = true;
                    if (list.size() == 0) {
                        rv.setVisibility(View.GONE);
                        llNone.setVisibility(View.VISIBLE);
                    }
                }

                if ("1".equals(type)) {
                    if (!isFirst) {
                        isFirst = true;
                    } else {
                        loadEnd();
                    }
                } else {
                    loadEnd();
                }

            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }


    private void loadEnd() {
        if (null != refreshLayout && refreshLayout.getState() == RefreshState.Refreshing)
            refreshLayout.finishRefresh(true);
        if (null != refreshLayout && refreshLayout.getState() == RefreshState.Loading)
            refreshLayout.finishLoadMore(true);
    }

    private void deleteMyReport(LogEntity logEntity, int position) {
        Map<String, String> map = new HashMap<>();
        map.put("reportCode", logEntity.getReportCode());
        map.put("reportType", logEntity.getReportType());
        MyHttpRequest.postRequest(Urls.deleteMyReport, this, SignUtils2.makeSignStr(map), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if ("200".equals(jsonObject.getString("code"))) {
                        MyToast.show("删除成功");
                        list.remove(position);
                        adapter.notifyItemRemoved(position);
                        adapter.notifyItemRangeChanged(position, list.size() - position);
                        HashMap<String, String> map = new HashMap<>();
                        map.put("reportCode", logEntity.getReportCode());
                        map.put("deleteLog", "true");
                        EventBus.getDefault().post(new MyEvent(map));
                        if (rv == null || llNone == null) {
                            return;
                        }
                        if (list.size() == 0) {
                            rv.setVisibility(View.GONE);
                            llNone.setVisibility(View.VISIBLE);
                        }
                    } else {
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    class LogListAdapter extends BaseQuickAdapter<LogEntity, BaseViewHolderEx> {
        private Context context;
        private GridSpacingItemDecoration gridSpacingItemDecoration;

        public LogListAdapter(int layoutResId, Context context, @Nullable List<LogEntity> data) {
            super(layoutResId, data);
            this.context = context;
            gridSpacingItemDecoration = new GridSpacingItemDecoration(4, Utils.dip2px(context, 8), false);
        }

        @Override
        protected void convert(BaseViewHolderEx helper, LogEntity item) {
            CircleImageView ivHead = helper.getView(R.id.ivHead);
            TextView tvName = helper.getView(R.id.tvName);
            TextView tvTime = helper.getView(R.id.tvTime);
            RelativeLayout rlMore = helper.getView(R.id.rlMore);
            TextView tv1 = helper.getView(R.id.tv1);
            TextView tv2 = helper.getView(R.id.tv2);
            TextView tv3 = helper.getView(R.id.tv3);
            TextView tv4 = helper.getView(R.id.tv4);
            TextView tv1Init = helper.getView(R.id.tv1Init);
            TextView tv2Init = helper.getView(R.id.tv2Init);
            TextView tv3Init = helper.getView(R.id.tv3Init);
            TextView tv4Init = helper.getView(R.id.tv4Init);
            LinearLayout ll3 = helper.getView(R.id.ll3);
            LinearLayout ll4 = helper.getView(R.id.ll4);
            RecyclerView imgRv = helper.getView(R.id.imgRv);
            LinearLayout llContent = helper.getView(R.id.llContent);
            LinearLayout llRead = helper.getView(R.id.llRead);
            TextView tvRead = helper.getView(R.id.tvRead);
            List<String> picList=new ArrayList<>();
            if (item.getPictureList().size() > 0||item.getVideoPictureList().size() > 0) {
                imgRv.setVisibility(View.VISIBLE);
                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imgRv.getLayoutParams();
                params.width = Utils.getScreenWidthPixel(mContext) - Utils.dip2px(mContext, 74);
                params.height = (Utils.getScreenWidthPixel(mContext) - Utils.dip2px(mContext, 98)) / 4;
                imgRv.setLayoutParams(params);
                GridLayoutManager gridLayoutManager1 = new GridLayoutManager(context, 4);
                imgRv.setLayoutManager(gridLayoutManager1);
                if (imgRv.getItemDecorationCount() == 0) {
                    imgRv.addItemDecoration(gridSpacingItemDecoration);
                }
            } else {
                imgRv.setVisibility(View.GONE);
            }
            if (item.getPictureList().size() > 0) {
                LogImagsAdapter adapter = null;
                picList.clear();
                if (item.getPictureList().size() >= 4) {
                    picList.addAll(item.getPictureList().subList(0, 4));
                    adapter = new LogImagsAdapter(R.layout.item_log_imges, context,picList,-1,item,helper.getItemPosition() );
                } else {
                    picList.addAll(item.getPictureList());
                    if(item.getVideoPictureList().size()>0){
                        if(item.getVideoPictureList().size()>=(4-item.getPictureList().size())){
                            picList.addAll(item.getVideoPictureList().subList(0, 4-item.getPictureList().size()));
                        }else{
                            picList.addAll(item.getVideoPictureList());
                        }
                    }
                    adapter = new LogImagsAdapter(R.layout.item_log_imges, context, picList,item.getPictureList().size(),item,helper.getItemPosition());
                }
                imgRv.setAdapter(adapter);
            } else {
                if (item.getVideoPictureList().size() >0) {
                    LogImagsAdapter adapter = null;
                    picList.clear();
                    if (item.getVideoPictureList().size() >= 4) {
                        picList.addAll(item.getVideoPictureList().subList(0, 4));
                    } else {
                        picList.addAll(item.getVideoPictureList());
                    }
                    adapter = new LogImagsAdapter(R.layout.item_log_imges, context,picList,0,item,helper.getItemPosition() );
                    imgRv.setAdapter(adapter);
                }
            }
            if (item.getPlayListPage().getList().size() > 0) {
                llRead.setVisibility(View.VISIBLE);
                String names = "";
                for (int i = 0; i < item.getPlayListPage().getList().size(); i++) {
                    if (i < 3) {
                        if (Utils.isNull(names)) {
                            names = names + "" + item.getPlayListPage().getList().get(i).getPlayEmployeeName();
                        } else {
                            names = names + "、" + item.getPlayListPage().getList().get(i).getPlayEmployeeName();
                        }
                    }
                }
                if (Utils.changeNumber(item.getPlayListPage().getTotal()) > 3) {
                    tvRead.setText(names + "等" + item.getPlayListPage().getTotal() + "人浏览过");
                } else {
                    tvRead.setText(names + item.getPlayListPage().getTotal() + "人浏览过");
                }
            } else {
                llRead.setVisibility(View.GONE);
            }
            GlideUtils.loadAvatar(mContext, item.getHeadPhotoThumbUrl(), ivHead);
            tvTime.setText(Utils.getAccurateDate(item.getCreateTime()));
            int sort = Utils.changeNumber(item.getSort());
            if (sort == 0) {
                sort = 1;
            }
            LinearLayout.LayoutParams tv1Params = (LinearLayout.LayoutParams) tv1Init.getLayoutParams();
            LinearLayout.LayoutParams tv2Params = (LinearLayout.LayoutParams) tv2Init.getLayoutParams();
            if ("0".equals(item.getReportType())) {
                tvName.setText(item.getEmployeeName() + Utils.getCurrentDay(item.getReportType(), item.getCreateTime()) + "(" + sort + ")");
                ll3.setVisibility(View.VISIBLE);
                ll4.setVisibility(View.VISIBLE);
                tv1Params.width = Utils.dip2px(mContext, 84);
                tv1Params.gravity = Gravity.RIGHT;
                tv2Params.width = Utils.dip2px(mContext, 84);
                tv2Params.gravity = Gravity.RIGHT;
                tv1Init.setText("今日完成工作：");
                tv2Init.setText("未完成工作：");
                tv3Init.setText("需协调工作：");
                tv4Init.setText("明日工作计划：");
                tv1.setText(item.getFinishWork());
                tv2.setText(item.getUnFinishWork());
                tv3.setText(item.getNeedHelpWork());
                tv4.setText(item.getNextWorkPlan());
            } else if ("1".equals(item.getReportType())) {
                tvName.setText(item.getEmployeeName() + Utils.getCurrentDay(item.getReportType(), item.getCreateTime()) + "(" + sort + ")");
                ll3.setVisibility(View.VISIBLE);
                ll4.setVisibility(View.VISIBLE);
                tv1Params.width = Utils.dip2px(mContext, 84);
                tv1Params.gravity = Gravity.RIGHT;
                tv2Params.width = Utils.dip2px(mContext, 84);
                tv2Params.gravity = Gravity.RIGHT;
                tv1Init.setText("本周完成工作：");
                tv2Init.setText("本周工作总结：");
                tv3Init.setText("需协调与帮助：");
                tv4Init.setText("下周工作计划：");
                tv1.setText(item.getFinishWork());
                tv2.setText(item.getUnFinishWork());
                tv3.setText(item.getNeedHelpWork());
                tv4.setText(item.getNextWorkPlan());
            } else if ("2".equals(item.getReportType())) {
                tvName.setText(item.getEmployeeName() + Utils.getCurrentDay(item.getReportType(), item.getCreateTime()) + "(" + sort + ")");
                ll3.setVisibility(View.VISIBLE);
                ll4.setVisibility(View.VISIBLE);
                tv1Params.width = Utils.dip2px(mContext, 84);
                tv1Params.gravity = Gravity.RIGHT;
                tv2Params.width = Utils.dip2px(mContext, 84);
                tv2Params.gravity = Gravity.RIGHT;
                tv1Init.setText("本月工作内容：");
                tv2Init.setText("本月工作总结：");
                tv3Init.setText("需帮助与支持：");
                tv4Init.setText("下月工作计划：");
                tv1.setText(item.getFinishWork());
                tv2.setText(item.getUnFinishWork());
                tv3.setText(item.getNeedHelpWork());
                tv4.setText(item.getNextWorkPlan());
            } else {
                tvName.setText(item.getEmployeeName() + Utils.getCurrentDay(item.getReportType(), item.getCreateTime()));
                ll3.setVisibility(View.GONE);
                ll4.setVisibility(View.GONE);
                tv1Params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
                tv1Params.gravity = Gravity.LEFT;
                tv2Params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
                tv2Params.gravity = Gravity.LEFT;
                tv1Init.setText("日志标题：");
                tv2Init.setText("日志内容：");
                tv1.setText(item.getTitle());
                tv2.setText(item.getContent());
            }
            tv1Init.setLayoutParams(tv1Params);
            tv2Init.setLayoutParams(tv2Params);
            rlMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    bottomListsDialog = new BottomListsDialog(context, dialogList, new SelectDialogListener() {
                        @Override
                        public void onCancel() {

                        }

                        @Override
                        public void onListItemClick(int position, List<String> list) {
                            KLog.i(type + "--" + position);
                            if ("0".equals(type)) {
                                //全部
                                if (position == 0) {
                                    if (Utils.isNull(singleEmployeeCode)) {
                                        singleEmployeeCode = item.getEmployeeCode();
                                        employeeCodes = item.getEmployeeCode();
                                        orgCodes = "";
                                        dialogList.set(0, "取消只看此人");
                                    } else {
                                        singleEmployeeCode = "";
                                        employeeCodes = "";
                                        orgCodes = "";
                                        dialogList.set(0, "只看此人");
                                    }
                                    isEnd = false;
                                    currentPage = 1;
                                    getLogData();
                                }
                            } else {
                                //本人
                                if (position == 0) {
                                    if (PermissionSpUtils.getBoolean(context,"12",false)) {
                                        Intent intent = new Intent(mContext, PublishLogsActivity.class);
                                        intent.putExtra("data", item);
                                        intent.putExtra("modify", true);
                                        startActivity(intent);
                                    } else {
                                        MyToast.show("管理员已关闭日志发布功能");
                                    }
                                } else if (position == 1) {
                                    new AlertDialog.Builder(mContext)
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                    deleteMyReport(item, helper.getItemPosition());
                                                }
                                            })
                                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            })
                                            .setMessage("确定要删除么?")
                                            .setTitle("提示")
                                            .show();

                                }
                            }

                            bottomListsDialog.dismiss();
                        }
                    });
                    bottomListsDialog.show();
                }
            });
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    readData(item, helper.getItemPosition());
                    Intent intent = new Intent(mContext, LogDetailActivity.class);
                    intent.putExtra("data", item);
                    intent.putExtra("title", tvName.getText().toString());
                    startActivity(intent);
                }
            });
            llContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    readData(item, helper.getItemPosition());
                    Intent intent = new Intent(mContext, LogDetailActivity.class);
                    intent.putExtra("data", item);
                    intent.putExtra("title", tvName.getText().toString());
                    startActivity(intent);
                }
            });
            imgRv.setOnTouchListener(new View.OnTouchListener() {
                //处理点击到RecyclerView上面无反应
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        float scaledHorizontalScrollFactor = ViewConfigurationCompat.getScaledHorizontalScrollFactor(ViewConfiguration.get(mContext), mContext);
                        float scaledVerticalScrollFactor = ViewConfigurationCompat.getScaledVerticalScrollFactor(ViewConfiguration.get(mContext), mContext);
                        if(scaledHorizontalScrollFactor>0||scaledVerticalScrollFactor>0){
//这里调用某个逻辑点击事件
                            llContent.performClick();
                            return true;
                        }
                    } return false;
                } });
            ivHead.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PersonalHomeActivity.class);
                    intent.putExtra("employeeCode", item.getEmployeeCode());
                    startActivity(intent);
                }
            });
        }

    }

    private void readData(LogEntity logEntity, int position) {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("reportCode", logEntity.getReportCode());
        allmap.put("reportType", logEntity.getReportType());
        allmap.put("reportEmployeeCode", logEntity.getEmployeeCode());
        MyHttpRequest.postRequest(Urls.getReportById, this, SignUtils2.makeSignStr(allmap), new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject jsonObject = new JSONObject(response.body().toString());
                    if(jsonObject.getString("code").equals("200")) {
                        JSONArray jsonArray=jsonObject.getJSONArray("data");
                        list.get(position).getPlayListPage().getList().clear();
                        for (int i = 0; i <jsonArray.length(); i++) {
                            JSONObject jsonObject1 =jsonArray.getJSONObject(i);
                            LogEntity.PlayListPageBean.ListBean listBean = new LogEntity.PlayListPageBean.ListBean();
                            listBean.setPlayEmployeeName(jsonObject1.getString("playEmployeeName"));
                            list.get(position).getPlayListPage().getList().add(listBean);
                        }
                        list.get(position).getPlayListPage().setTotal(jsonObject.getString("total"));
                        adapter.notifyItemChanged(position);
                    }else{
                        if (jsonObject.getString("code").startsWith("400")) {
                            OkGoUtil.onErrorIntent(jsonObject.getString("code"),jsonObject.getString("msg"));
                        }else{
                            MyToast.show(jsonObject.getString("msg"));
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            Map<String, String> map = messageEvent.getHashMap();
            if (map.containsKey("deleteLog")) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getReportCode().equals(map.get("reportCode"))) {
                        list.remove(i);
                        adapter.notifyItemRemoved(i);
                        adapter.notifyItemRangeChanged(i, list.size() - i);
                        if (rv == null || llNone == null) {
                            return;
                        }
                        if (list.size() == 0) {
                            rv.setVisibility(View.GONE);
                            llNone.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }else if(map.containsKey("Log_Read")){
                int index=Utils.changeNumber(map.get("Log_Read"));
                readData(list.get(index),index);
            }
        } else {
            String msg = messageEvent.getMessage();
            if (Utils.isNull(msg)) {
                return;
            }
            if ("RefreshLogFragment".equals(msg)) {
                //刷新最近列表
                isEnd = false;
                currentPage = 1;
                getLogData();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();

    }

}
